package vn.sunnet.game.SinhTon.screen;

import vn.sunnet.game.SinhTon.global.Assets;
import vn.sunnet.game.SinhTon.global.OverlapTester;
import vn.sunnet.game.SinhTon.global.Setting;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class MenuScreen extends BaseScreen {
	private Button btnMusic, btnSound;;
	private Button btnExit, btnInfo, btnDownload, btnShare, btnVibrate;

	private float x_TouchDown, y_TouchDown;
	private boolean isPlay;
	private boolean isTouchDown;
	private Animation animation;
	private float stateTime;
	private TextureRegion trCurrentFrame;
	private Rectangle btPlay;

	public MenuScreen(MyGame myGame) {
		super(myGame);
		// Lay ve cac anh, nhac, font dung trong man hinh MenuScreen
		if (!Assets.GET_MAINMENU)
			Assets.getMainMenu();

		btPlay = new Rectangle(331, 180, 152, 146);
		animation = new Animation(0.2f, Assets.arrPlay1);

		Image playImage = new Image(Assets.ttPlay);
		playImage.setPosition(331, 180);

		playImage.setOrigin(76, 73);
		playImage.setColor(1, 1, 1, 0);

		// Cho anh nut Play hien len trong 2s va quay theo tam anh trong 4s
		// playImage.addAction(Actions.parallel(Actions.fadeIn(2),
		// Actions.rotateBy(360, 4)));

		// forever: để chạy liên tục,
		// parallel la chay song song cac hanh dong
		playImage.addAction(Actions.parallel(Actions.fadeIn(2),
				Actions.forever(Actions.rotateBy(360, 4))));
		// playImage.addAction(Actions.sequence(Actions.delay(1),Actions.parallel(Actions.fadeIn(10),Actions.rotateBy(360,1))));
		stage.addActor(playImage);

		// Khoi tao cac Image button
		initButton();
		Assets.mLoading.pause();
		if (Setting.isSound(Setting.MUSIC))
			Assets.mMainMenu.play();
		else
			Assets.mMainMenu.pause();
	}

	@Override
	public void render(float delta) {
		stage.act();
		stateTime += 0.5 * Gdx.graphics.getDeltaTime();
		trCurrentFrame = animation.getKeyFrame(stateTime, true);
		batch.begin();
		batch.draw(Assets.ttBgMenu, 0, 0);
		batch.draw(trCurrentFrame, 342, 192, 127, 124);
		Assets.effectStar.draw(batch, 0.02f);

		if (isPlay)
			batch.draw(Assets.ttPlay_hover, 331, 180, 152, 146);
		batch.end();
		stage.draw();
		update();
	}

	private void update() {
		if (isPlay && !isTouchDown) {
			isPlay = false;
			// Chuyển màn hình
			// game.setScreen(game.selectMap);
			game.setScreen(new SelectMap(game));
		}
	}

	@Override
	public void show() {
		super.show();
	}

	@Override
	public void pause() {
		super.pause();
	}

	@Override
	public void resume() {
		super.resume();
	}

	@Override
	public void dispose() {
		super.dispose();
//		Assets.disposeObjects();
		Assets.mLoading.dispose();
		Assets.mMainMenu.dispose();
		Assets.ttPlay_hover.dispose();
		Assets.ttPlay.dispose();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.BACK)
			game.dialog.onExit();
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		isTouchDown = true;
		x_TouchDown = screenX * Assets.SCREEN_WIDTH / Gdx.graphics.getWidth();
		y_TouchDown = (Gdx.graphics.getHeight() - screenY)
				* Assets.SCREEN_HEIGHT / Gdx.graphics.getHeight();
		if (OverlapTester.pointInRectangle(btPlay, x_TouchDown, y_TouchDown)) {
			if (Setting.getBoolean(Setting.SOUND))
				Assets.sClick.play(1);
			isPlay = true;
		}
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		isTouchDown = false;
		return false;
	}

	private void initButton() {
		// Nut thong tin
		btnInfo = new Button(Assets.skin, "btnInfo");
		btnInfo.setBounds(622, 422, 50, 50);
		btnInfo.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				game.dialog.onInfo();
			}
		});
		stage.addActor(btnInfo);
		
		// Nut download
		btnDownload = new Button(Assets.skin, "btnDownload");
		btnDownload.setBounds(678, 418, 50, 55);
		btnDownload.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				game.dialog.onDownload();
			}
		});
		stage.addActor(btnDownload);

		// Nut Share
		btnShare = new Button(Assets.skin, "btnShare");
		btnShare.setBounds(733, 421, 56, 52);
		btnShare.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				game.dialog.onShare();
			}
		});
		stage.addActor(btnShare);

		// Nut exit
		btnExit = new Button(Assets.skin, "btnExit");
		btnExit.setBounds(22, 11, 162, 58);
		btnExit.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				game.dialog.onExit();
			}
		});
		stage.addActor(btnExit);

		// Nut Vibrate
		btnVibrate = new Button(Assets.skin.getDrawable("vibrate"),
				Assets.skin.getDrawable("nonvibrating"),
				Assets.skin.getDrawable("nonvibrating"));
		btnVibrate.setBounds(715, 16, 66, 61);
		btnVibrate.setChecked(!Setting.getBoolean(Setting.VIBRATE));
		btnVibrate.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				boolean isVibrate = Setting.getBoolean(Setting.VIBRATE);
				Setting.putBoolean(Setting.VIBRATE, !isVibrate);
			}
		});
		stage.addActor(btnVibrate);
		
		// Nut Sound
		btnSound = new Button(Assets.skin.getDrawable("soundOn"),
				Assets.skin.getDrawable("soundOff"),
				Assets.skin.getDrawable("soundOff"));
		btnSound.setBounds(642, 57, 70, 63);
		btnSound.setChecked(!Setting.getBoolean(Setting.SOUND));
		btnSound.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				boolean isSound = Setting.getBoolean(Setting.SOUND);
				Setting.putBoolean(Setting.SOUND, !isSound);
			}
		});
		stage.addActor(btnSound);
		
		// Nut Music
		btnMusic = new Button(Assets.skin.getDrawable("musicOn"),
				Assets.skin.getDrawable("musicOff"),
				Assets.skin.getDrawable("musicOff"));
		btnMusic.setBounds(712, 99, 70, 63);
		btnMusic.setChecked(!Setting.getBoolean(Setting.MUSIC));
		btnMusic.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if(Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				boolean isMusic = Setting.getBoolean(Setting.MUSIC);
				Setting.putBoolean(Setting.MUSIC, !isMusic);
				if (Setting.isSound(Setting.MUSIC))
					Assets.mMainMenu.play();
				else
					Assets.mMainMenu.pause();
			}
		});
		stage.addActor(btnMusic);
	}
}
